// JavaScript Document
$(function () {
    resizeBoard();
    $(window).bind("resize", resizeBoard);
    bindTab(".drug .ybTabTitle", ".ybName", ".drug .ybTabContent");
    bindTab(".doctor .ybTabTitle", ".ybName", ".doctor .ybTabContent");
    bindBoardEvent();
    bindSearch();
    bindClose();
    loadAllServerData();
});

function loadAllServerData() {
    var thisYear=new Date().getFullYear();
    //慢病省医保加载
    loadServerData("/standard/chronicRule/queryCount/1", null, function (data) {
        $("#loadChronicRuleCountAll1").html(data.datas[0].countAll);
        $("#loadChronicRuleCount1").html(data.datas[0].countHospital);
    }, "mxjb");
    //慢病市医保加载
    loadServerData("/standard/chronicRule/queryCount/2", null, function (data) {
        $("#loadChronicRuleCountAll2").html(data.datas[0].countAll);
        $("#loadChronicRuleCount2").html(data.datas[0].countHospital);
    }, "mxjb");
    //单病省医保加载
    loadServerData("/standard/drgRule/queryCount/1", null, function (data) {
        $("#loadDrgRuleCountAll1").html(data.datas[0].countAll);
        $("#loadDrgRuleCount1").html(data.datas[0].countHospital);
    }, "dbz");
    //单病市医保加载
    loadServerData("/standard/drgRule/queryCount/2", null, function (data) {
        $("#loadDrgRuleCountAll2").html(data.datas[0].countAll);
        $("#loadDrgRuleCount2").html(data.datas[0].countHospital);
    }, "dbz");

    //日间省医保加载
    loadServerData("/standard/dayoperationRule/queryCount/1", null, function (data) {
        $("#loadDayoperationRuleCountAll1").html(data.datas[0].countAll);
        $("#loadDayoperationRuleCount1").html(data.datas[0].countHospital);
    }, "rjss");
    //日间市医保加载
    loadServerData("/standard/dayoperationRule/queryCount/2", null, function (data) {
        $("#loadDayoperationRuleCountAll2").html(data.datas[0].countAll);
        $("#loadDayoperationRuleCount2").html(data.datas[0].countHospital);
    }, "rjss");

    //用药省医保加载
    loadServerData("/standard/drugRule/queryCount/1", null, function (data) {
        $("#loadDrugRuleCountAll1").html(data.datas[0].countAll);
        $("#loadDrugRuleCount1").html(data.datas[0].countHospital);
    }, "yy");
    //用药省医保加载-0001
    loadServerData("/standard/drugRule/queryCount/1/0001", null, function (data) {
        $("#detail0001 .bar").css("width",data.datas[0].countHospital/data.datas[0].countAll*100+"%");
        if(data.datas[0].countHospital/data.datas[0].countAll>0.9){
            $("#detail0001 .bar").css("border-top-right-radius","0.8em");
            $("#detail0001 .bar").css("border-bottom-right-radius","0.8em");
        }
        $("#detail0001 .right").html(data.datas[0].countHospital+"/"+data.datas[0].countAll);
    }, "yy");
    //用药省医保加载-0002
    loadServerData("/standard/drugRule/queryCount/1/0002", null, function (data) {
        $("#detail0002 .bar").css("width",data.datas[0].countHospital/data.datas[0].countAll*100+"%");
        if(data.datas[0].countHospital/data.datas[0].countAll>0.9){
            $("#detail0002 .bar").css("border-top-right-radius","0.8em");
            $("#detail0002 .bar").css("border-bottom-right-radius","0.8em");
        }
        $("#detail0002 .right").html(data.datas[0].countHospital+"/"+data.datas[0].countAll);
    }, "yy");
    //用药省医保加载-0003
    loadServerData("/standard/drugRule/queryCount/1/0003", null, function (data) {
        $("#detail0003 .bar").css("width",data.datas[0].countHospital/data.datas[0].countAll*100+"%");
        if(data.datas[0].countHospital/data.datas[0].countAll>0.9){
            $("#detail0003 .bar").css("border-top-right-radius","0.8em");
            $("#detail0003 .bar").css("border-bottom-right-radius","0.8em");
        }
        $("#detail0003 .right").html(data.datas[0].countHospital+"/"+data.datas[0].countAll);
    }, "yy");
    //用药省医保加载-0007
    loadServerData("/standard/drugRule/queryCount/1/0007", null, function (data) {
        $("#detail0007 .bar").css("width",data.datas[0].countHospital/data.datas[0].countAll*100+"%");
        if(data.datas[0].countHospital/data.datas[0].countAll>0.9){
            $("#detail0007 .bar").css("border-top-right-radius","0.8em");
            $("#detail0007 .bar").css("border-bottom-right-radius","0.8em");
        }
        $("#detail0007 .right").html(data.datas[0].countHospital+"/"+data.datas[0].countAll);
    }, "yy");
    //用药市医保加载
    loadServerData("/standard/drugRule/queryCount/2", null, function (data) {
        $("#loadDrugRuleCountAll2").html(data.datas[0].countAll);
        $("#loadDrugRuleCount2").html(data.datas[0].countHospital);
    }, "yy");

    //指标省医保加载
    loadServerData("/standard/totalRule/queryCount/1/"+thisYear, null, function (data) {
        $("#loadTotalRuleCountAll1").html(data.datas[0].countAll);
        $("#loadTotalRuleCount1").html(data.datas[0].countHospital);
    }, "zb");
    //指标市医保加载
    loadServerData("/standard/totalRule/queryCount/2/"+thisYear, null, function (data) {
        $("#loadTotalRuleCountAll2").html(data.datas[0].countAll);
        $("#loadTotalRuleCount2").html(data.datas[0].countHospital);
    }, "zb");

    //指标丙类
    loadServerData("/standard/totalRule/queryCountIndex/4/"+thisYear, null, function (data) {
        $("#bl1A").html((data.datas[0].micalvalue*100).toFixed(2));
        $("#bl1B").html((data.datas[0].warnvalue*100).toFixed(2));
        $("#bl1C").html((data.datas[0].checkvalue*100).toFixed(2));
        $("#bl2A").html((data.datas[1].micalvalue*100).toFixed(2));
        $("#bl2B").html((data.datas[1].warnvalue*100).toFixed(2));
        $("#bl2C").html((data.datas[1].checkvalue*100).toFixed(2));
    }, "bl");
    //指标总额
    loadServerData("/standard/totalRule/queryCountIndex/1/"+thisYear, null, function (data) {

        // $("#ze1").html(data.datas[0].micalvalue.toLocaleString()+"元");
        // $("#ze2").html(data.datas[1].micalvalue.toLocaleString()+"元");
        $("#ze1").html("￥"+(data.datas[0].micalvalue/10000).toFixed(2)+"万元");
        $("#ze2").html("￥"+(data.datas[1].micalvalue/10000).toFixed(2)+"万元");
    }, "ze");
}

function loadServerData(url, data, render, loadingParent) {
    showLoading($("#" + loadingParent));
    $.ajax({
        url: url,
        data: {
            datas: JSON.stringify(data)
        },
        dataType: "json",
        success: function (ret) {
            hideLoading($("#" + loadingParent));
            render(ret);
        },
        error: function (ret) {
            alert("发生错误");
            console.debug(ret);
        }
    });
}

var listParam = {
    url: "",
    data: {},
    onclick: function (item) {
    },
    columns: [{
        title: "示例",
        width: "100",
        align: "left",
        data: "id",
        render: function (item) {
            return item.id;
        }
    }]
};

function showListWindow(param,showCheck) {
    listParam = param;
    $(".listWindow").show();
    $(".listWindow").css({"width": "84%", "left": "calc( 8% )", "top": "6%", "height": "85%", "opacity": "1"});
    $(".listWindow .title").html(param.title);
    $(".listWindow .listFoot .info").html(param.footInfoHtml);
    loadServerData(param.url, param.data, function (data) {
        renderListData(data);//totalSize  datas
    })
    $(".listWindow td").bind("click", function () {
        $(".detailWindow").show();
    });
    if(showCheck)
        $(".checkGroup").show();
    else
        $(".checkGroup").hide();

}

function renderListData(retData) {
    var datas = retData.datas;
    var countAll = retData.totalSize;
    var count = retData.datas.length;
    var countPage = Math.ceil(countAll / listParam.data.pageSize);
    listParam.data.countPage = countPage;
    listParam.data.countAll = countAll;
    loadGrid(datas);
    loadPage();
}

function bindSearch() {
    var buttonGroup = $(".listWindow .buttonGroup");
    buttonGroup.children().bind("mousedown", function (e) {
        $(e.target).addClass("mousedown");
    })
    buttonGroup.children().bind("mouseup", function (e) {
        $(e.target).removeClass("mousedown");
    })
    buttonGroup.children().bind("click", function (e) {
        var index = $(e.target).index();
        buttonGroup.find(".selected").removeClass("selected");
        buttonGroup.children().eq(index).addClass("selected");
        var ybType = index;
        listParam.data.ybType = index;
        //加入footInfo
        try {
            listParam.footInfoHtml = listParam.footInfo[index - 1].innerHTML;
        } catch (e) {
            listParam.footInfoHtml = "";
        }
        showListWindow(listParam);
        e.stopPropagation();
    })

    var searchButton = $(".listWindow .searchBox .button");
    var searchInput = $(".listWindow .searchBox input");
    searchButton.bind("click", function (e) {
        var keyWord = searchInput.val();
        listParam.data.keyWord = encodeURI(keyWord);
        var showCheck=$(".listWindow .title").html()=="用药限制";
        showListWindow(listParam,showCheck);
        e.stopPropagation();
    })
    $(".listWindow .input input").bind("keypress", function (e) {
        if (e.which == 13) {
            $(".listWindow .searchBox .button").trigger("click");
        }
    });

    var checkGroup = $(".listWindow .checkGroup");
    checkGroup.children().bind("click", function (e) {
        var index = $(e.target).index();
        if(index==0){
            checkGroup.children().addClass("selected am-icon-check-square");
            checkGroup.children().eq(0).removeClass("selected");
            checkGroup.children().eq(1).removeClass("selected am-icon-check-square");
        }else if(index==1){
            $( checkGroup.children()).each(function(i,div){
                if(i>1){
                    if($(div).hasClass("selected am-icon-check-square"))
                        $(div).removeClass("selected am-icon-check-square");
                    else    $(div).addClass("selected am-icon-check-square");
                }
            })
        }else{
            if($(e.target).hasClass("selected"))
                checkGroup.children().eq(index).removeClass("selected am-icon-check-square");
            else    checkGroup.children().eq(index).addClass("selected am-icon-check-square");

        }

        var checked=$(".listWindow .checkGroup").find(".selected");
        var checkedParam="";
        for(var i=0;i<checked.length;i++){
            checkedParam+=(",'"+checked.eq(i).attr("value")+"'");
        }
        listParam.data.checkedParam=(checkedParam.length>0?checkedParam.substr("1"):"");
        showListWindow(listParam,true);
        e.stopPropagation();
    })
    checkGroup.children().bind("mousedown", function (e) {
        $(e.target).addClass("mousedown");
    })
    checkGroup.children().bind("mouseup", function (e) {
        $(e.target).removeClass("mousedown");
    })
}

function loadGrid(datas) {
    var table = $(".listWindow .tableContainer table");
    table.html("");
    $(datas).each(function (index, row) {
        var tr = $("<tr></tr>");
        tr.bind("click", function () {
            listParam.onclick(row);
        });
        $(listParam.columns).each(function (index, item) {
            if (item.render == null) {
                tr.append($("<td width=" + item.width + " align='" + item.align + "'>" + row[item.data] + "</td>"));
            } else {
                tr.append($("<td width=" + item.width + " align='" + item.align + "'></td>").append(item.render(row)));
            }
        });
        table.append(tr);
    });
}

function gotoPage(e, page) {
    if (page == null) {
        page = $(e.target).text();
    }
    if (page==0||page > listParam.data.countPage) return;
    listParam.data.pageNum = page;
    loadServerData(listParam.url, listParam.data, function (data) {
        renderListData(data);//totalSize  datas
    },"listBody")
}

function loadPage() {
    var nowPage = listParam.data.pageNum;
    var pageCount = listParam.data.countPage;
    var countAll = listParam.data.countAll;
    var startPage = 1;
    var ul = $(".listWindow .pagerInfo");
    ul.html("");
    ul.append($("<span>共" + countAll + "条记录  " + pageCount + "页</span>"));

    ul.append($("<span class='right'><a>末页</a></span>").bind("click", function () {
        gotoPage(null, pageCount)
    }));
    ul.append($("<span class='right'><a>下一页</a></span>").bind("click", function () {
        gotoPage(null, listParam.data.pageNum + 1)
    }));
    var pageBox = $($("<span class='right'></span>"));
    ul.append(pageBox);
    ul.append($("<span class='right'><a>上一页</a></span>").bind("click", function () {
        gotoPage(null, listParam.data.pageNum - 1)
    }));
    ul.append($("<span class='right'><a>首页</a></span>").bind("click", function () {
        gotoPage(null, 1)
    }));

    var loopSize = 5;
    if (listParam.data.pageNum > 3) {
        startPage = nowPage - 2;
    }
    if (listParam.data.pageNum >= pageCount - 2) {
        startPage = pageCount - 4;
    }
    if (pageCount <= 5) {
        startPage = 1;
    }
    for (var i = startPage; i < pageCount + 1 && i < startPage + loopSize; i++) {
        if (nowPage == i)
            pageBox.append($('<a class="active">' + i + '</a>'));
        else {
            pageBox.append($('<a>' + i + '</a>').bind("click", gotoPage));
        }
    }
}

function resizeBoard() {
    var baseWidth = $("#lineF .infoBox").width();
    height = baseWidth * 250 / 480;
    $(".infoBox").height(height + "px");
    $(".infoBox").css("font-size", height / 2 + "px");

    $("h1").css("font-size", height / 9 + "px");
    $("h2").css("font-size", height / 10 + "px");
    $("h3").css("font-size", height / 15 + "px");
    $("h4").css("font-size", height / 7 + "px");
    $(".barBox").css("font-size", height / 15 + "px");
    $(".listWindow").css("font-size", height / 12 + "px");
    $(".listWindow .toolBar").css("font-size", height / 12 + "px");
    //detail页面的tab页面高度自适应
    resizeDetail();
}

function bindTab(buttonGroup, buttomClass, contentGroup) {
    $(buttonGroup).children().bind("click", function (e) {
        if ($(e.target).hasClass(buttomClass))
            index = $(e.target).index();
        else index = $(e.target).parents(buttomClass).index();
        $(contentGroup).find(".active").removeClass("active");
        $(buttonGroup).find(".active").removeClass("active");
        $(contentGroup).children().eq(index).addClass("active");
        $(buttonGroup).children().eq(index).addClass("active");
        e.stopPropagation();
    });
    $(buttonGroup).children().eq(0).addClass("active");
    $(contentGroup).children().eq(0).addClass("active");
}

function bindBoardEvent() {
    $(".shade").hide();
    $(".listWindow").hide();
    $(".detailWindow").hide();
    $(".detailWindow2").hide();
    $(".shade").bind("click", function (e) {
        if ($(e.target).hasClass("shade"))
            hideShade();
    });
    // $("#pkrk").bind("click",function(){
    //     top.addTabs();
    // });
    // $("#khzb").bind("click",function(){
    //     top.addTabs({
    //         id: '9999',
    //         title: '综管指标规则',
    //         close: 'ture',
    //         url: '/standard/totalRule'
    //     });
    // });
    // $("#jjze").bind("click",function(){
    //     top.addTabs();
    // });
    //慢病列表加载
    mxjbBoard();
    //单病种列表加载
    dbzBoard();
    //日间手术列表加载
    rjssBoard();
    //用药列表加载
    yyBoard();
    //指标列表加载
    zbBoard();

    //总额指标加载
    zeBoard();
}

function showShade() {
    $("body").css("overflow", "hidden");
    $(".shade").fadeIn(300);
}

function hideShade() {
    $("body").css("overflow", "auto");
    $(".shade").fadeOut(300);
    listWindowHide();
    hideDetail();
    hideDetail2();
}

function bindClose() {
    var exit = $(".exit");
    exit.bind("click", function (e) {
        var listWindow = $(e.target).parents(".listWindow");
        if (listWindow.length > 0) {
            hideShade();
            listWindowHide();
        }
        if($(e.target).parents(".detailWindow")!=null){
            hideDetail();
            hideDetail2();
        };
    })
    var close = $(".close");
    close.bind("click", function (e) {
        var listWindow = $(e.target).parents(".listWindow");
        if (listWindow.length > 0) {
            hideShade();
            listWindowHide();
        }
        if($(e.target).parents(".detailWindow")!=null){
            hideDetail();
            hideDetail2();
        };
    })
}

function showLoading(parent) {
    if (parent.find(".loading").length == 0)
        parent.append($("<div class='loading'></div>"));
}

function hideLoading(parent) {
    parent.find(".loading").fadeOut(300, function () {
        $(this).remove();
    });
}

function listWindowHide() {
    $(".listWindow").css({"width": "80%", "left": "calc( 10% )", "top": "10%", "height": "80%", "opacity": "0"});
    setTimeout(function () {
        $(".listWindow").hide();
    }, 300)
}
function showDetail2(){
    $(".detailWindow2").show();
    $(".detailWindow2").css({"left": "20px", "top": "20px", "bottom": "20px", "right": "20px", "opacity": "1"});
}
function hideDetail2(){
    $(".detailWindow2").css({"left": "60px", "top": "60px", "bottom": "60px", "right": "60px", "opacity": "0"});
    setTimeout(function () {
        $(".detailWindow2").hide();
    }, 300)
}